#
# DEPRECATED use move_files.cmake
# Author: pepone.onrez@gmail.com
# License: GPL-v3
#
# This cmake script generate rules for copy generated files to convenient places
# in the project structure.
# There are to macros on for move the headers other for move sources.
# Maybe a single macro with the directory as a input param is more convenient here
#

MACRO( APPEND ORIGINAL NEW_BIT )

  IF    ( NOT ${ORIGINAL} )
    SET( ${ORIGINAL} ${NEW_BIT} )
  ELSE  ( NOT ${ORIGINAL} )
    SET( ${ORIGINAL} ${${ORIGINAL}} ${NEW_BIT} )
  ENDIF ( NOT ${ORIGINAL} )

ENDMACRO( APPEND ORIGINAL NEW_BIT )

# This macro take is input arg as a file name and generate a rule for copy to src/generated
# if the file has changed. It alos generate a custo target GENERATED_SOURCES that you can DEPEND on
# for force the commands being call

MACRO ( MOVE_FILES OUTPUT_FILES )
    SET(PARAM_LIST "")
    SET(GENERATED_FILES "")
    FOREACH(PARAM ${ARGN})
        LIST(APPEND PARAM_LIST ${PARAM})
    ENDFOREACH(PARAM ${ARGN})

    LIST(LENGTH PARAM_LIST LIST_SIZE)

    IF(LIST_SIZE GREATER 2)
        LIST(GET PARAM_LIST 0 GENERATED_TARGET)
        MESSAGE(STATUS "Target name to: ${GENERATED_TARGET}")
        
        LIST(GET PARAM_LIST 1 TARGET_DIR)
        IF(NOT EXISTS ${TARGET_DIR}/ )
            MESSAGE(ERROR "Target dir: ${TARGET_DIR} not exists !!")
        ELSE(NOT EXISTS ${TARGET_DIR}/ )
            MESSAGE(STATUS "Target dir: ${TARGET_DIR}")
        ENDIF(NOT EXISTS ${TARGET_DIR}/ )

        MATH(EXPR UP_LMIT "${LIST_SIZE} - 1")
        FOREACH(CONT RANGE 2 ${UP_LMIT})
            LIST(GET PARAM_LIST ${CONT} FILE_NAME)
            MESSAGE(STATUS "file: ${FILE_NAME}")

            GET_FILENAME_COMPONENT( BASE_NAME ${FILE_NAME} NAME )
            LIST(APPEND GENERATED_FILES ${TARGET_DIR}/${BASE_NAME} )

            ADD_CUSTOM_COMMAND(
                OUTPUT ${TARGET_DIR}/${BASE_NAME}
                DEPENDS ${FILE_NAME}
                COMMAND ${CMAKE_COMMAND} -E copy_if_different ${FILE_NAME} ${TARGET_DIR}/${BASE_NAME}
                COMMENT " --- Copying file ${BASE_NAME} ")

            MESSAGE(STATUS "ouput file: ${TARGET_DIR}/${BASE_NAME}")

        ENDFOREACH(CONT RANGE 2 ${UP_LMIT})

        MESSAGE(STATUS "Add target: ${GENERATED_TARGET}")

        MESSAGE(STATUS "Depends on: ${GENERATED_FILES}")
        ADD_CUSTOM_TARGET(${GENERATED_TARGET} DEPENDS ${GENERATED_FILES})

        SET(OUTPUT_FILES ${GENERATED_FILES})

    ELSE(LIST_SIZE GREATER 2)
        MESSAGE(STATUS "usage MOVE_SOURCES(<target-name> <target-dir> <file-1> ... <file-n>)")
    ENDIF(LIST_SIZE GREATER 2)

ENDMACRO ( MOVE_FILES OUTPUT_FILES )
